import requests
from urllib.parse import quote

from bs4 import BeautifulSoup
import mysql.connector

def pa():
    #学校名称
    schoolName = "中国海洋大学"

    #爬取链接
    url = "http://career.ouc.edu.cn/zftal-web/zfjy!ykfw!zghydx10423/xjhxx_cxXjhListQtcx.html?doType=query&queryModel.currentPage=1&queryModel.showCount=32000"

    #请求头
    headers = {

    }
    #爬取数据并解析
    html = requests.get(url=url, headers=headers)

    #解析JSON字符串,获取数据列表
    dataList = html.json().get("items")

    for data in dataList:
        #获取JSON字符串中指定的键值对
        print(data.get("xjhmc")+"|"+data.get("sqsj"))
        mysql_conn(data.get("xjhmc"),schoolName,data.get("sqsj"))


#数据库连接
def mysql_conn(job_title,school_name,create_time):
    mydb = mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        passwd="",
        database="hschool_job"
    )
    mycursor = mydb.cursor()

    #插入
    sql = "INSERT INTO h_job (job_title,school_name,create_time) VALUES (%s,%s,%s)"
    val = (job_title,school_name,create_time)
    mycursor.execute(sql, val)
    mydb.commit()

if __name__ == '__main__':
    pa()
